package com.luck.easyfoodbatch.ui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpCenterActivity extends BaseActivity {

    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;
    @BindView(R.id.rl_shop_http)
    RelativeLayout rlShopHttp;
    @BindView(R.id.rl_easy_http)
    RelativeLayout rlEasyHttp;
    @BindView(R.id.rl_privacy)
    RelativeLayout rlPrivacy;
    @BindView(R.id.rl_phone)
    RelativeLayout rl_phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_center, "帮助中心", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rl_about, R.id.rl_shop_http, R.id.rl_easy_http, R.id.rl_privacy, R.id.rl_phone})
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.rl_about:
                bundle.putInt("type", 1);
                bundle.putString("title", "关于易食批");
                startAct(HelpBaseActivity.class, bundle);
                break;
            case R.id.rl_shop_http:
                bundle.putInt("type", 2);
                bundle.putString("title", "购物流程");
                startAct(HelpBaseActivity.class, bundle);
                break;
            case R.id.rl_easy_http:
                bundle.putInt("type", 3);
                bundle.putString("title", "易食批用户协议");
                startAct(HelpBaseActivity.class, bundle);
                break;
            case R.id.rl_privacy:
                bundle.putInt("type", 4);
                bundle.putString("title", "隐私声明");
                startAct(HelpBaseActivity.class, bundle);
                break;
            case R.id.rl_phone:
                onCall(HttpUtil.CALL_PHONE);
                //startAct(OpenBillingActivity.class);
                //  showPhoneDialog();
                break;
        }
    }
    /**
     获取相应权限
     * @param activity
     * @param permission
     * @param requestCode
     * @return true为已有该权限
     */

    /**
     * 拨打电话
     */
    private void showPhoneDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_base_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        tv_title.setText("联系客服");
        btn_commit.setText("呼叫");
        tv_content.setText(HttpUtil.CALL_PHONE);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.callPhone(HttpUtil.CALL_PHONE, mContext);
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //拨打电话权限管理
    final public static int REQUEST_CODE_ASK_CALL_PHONE = 123;

    public void onCall(String mobile){
        if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(mContext,Manifest.permission.CALL_PHONE);
            if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(HelpCenterActivity.this,new String[]{Manifest.permission.CALL_PHONE},REQUEST_CODE_ASK_CALL_PHONE);
                //Tools.callPhone(mobile,mContext);
                return;
            }else{
                showPhoneDialog();
                //Tools.callPhone(mobile,mContext);
            }
        } else {
            showPhoneDialog();
            // Tools.callPhone(mobile,mContext);
        }
    }

}
